/*
 * SetCameraDefault
 * code is similar same as SetCameraState, except that SetCameraDefault is called 
 * sets the user default (setting that remains when the camera is shut down) to various
 * settings, this includes current, factory, min, max and Enum List values
 */
 
function SetCameraDefaultDescription( FuncPtr )
{

        FuncPtr = printf   // Fixed 6/2011


	FuncPtr('# SetCameraDefault(UpdateSource, PName, default)');
/*
	FuncPtr('# has 3 possible values for UpdateSource ');
	FuncPtr('# if Update Source is ');
	FuncPtr('# 0: use factory default ');
	FuncPtr('# 1: use current value ');
	FuncPtr('# 2: use value in the default parameter ');
	FuncPtr('# this script gets the current and factory settings');
	FuncPtr('# then sets the camera user default to');
	FuncPtr('# these values  and gets the camera user default for verification');
	FuncPtr('# note that the camera default is the value which is retained when the ');
	FuncPtr('# camera is turned off ');
	FuncPtr('# ');
	
	FuncPtr('# the sequence of calls is thus : ');
	FuncPtr('# FOR CURRENT: ');
	FuncPtr('# GetCameraState(param,...)  ');
	FuncPtr('# SetCameraDefault(1,param,...)  ');
	FuncPtr('# GetCameraDefault(1,param,...)  ');
	FuncPtr('# FOR FACTORY: '); 
	FuncPtr('# SetCameraDefault(0,param,...) ');
	FuncPtr('# GetCameraDefault(0,param,...) ');
	FuncPtr('# GetCameraDefault(1,param,...) ');

	
	FuncPtr('# OUTPUT FORMAT: ');
	FuncPtr('# acpd[0][0][0] 600 (current) 600 (user default) ');
	FuncPtr('# acpd[0][0][0] 600 (factory) 600 (user default) ');	
        FuncPtr('# the values in the [ ] brackets are status returns from ');
        FuncPtr('# the API calls  ');
	FuncPtr('# after the current and factory values , arbitrary  values are used,');
	FuncPtr('# typically these are the minimum and maximum possible values');
	FuncPtr('# the status values are from SetCameraDefault, GetCameraDefault ');
	FuncPtr('#  ');
	FuncPtr('# acpd[0][0] 5 (min) (5) ');
	FuncPtr('# acpd[0][0] 65535 (max) (65535) ');

	FuncPtr('# READ only tags  are noted ');
	FuncPtr('# ');
*/
} 

function SetCameraDefault(a)
{
	flashpoint();
	
	SetCameraDefaultDescription(printf);

	var usetVar=new Array("uset0","uset1","uset2","uset3","uset4","uset5","uset6","uset7" );
	var isetVar=new Array("iset0","iset1","iset2","iset3","iset4","iset5","iset6","iset7" );
	var ssetVar=new Array("sset0","sset1","sset2","sset3","sset4","sset5","sset6","sset7" );
	
	var setTitle=new Array("title0","title1","title2","title3","title4","title5","title6","title7" );

	var i,setting;
	
	writeMenu('"SetCameraDefault"','"CMD"','"SetCameraDefault()"');
	writeVariables();
	printf('declare s: readwrite ');

	print('declare u: uset0');
	for(i=1 ;i< usetVar.length ; i++) print(",",usetVar[i]); printf(' ');
	print('declare i: iset0');
	for(i=1 ;i< isetVar.length ; i++) print(",",isetVar[i]); printf(' ');
	print('declare s: sset0');
	for(i=1 ;i< ssetVar.length ; i++) print(",",ssetVar[i]); printf(' ');

	print('declare s: title0');	
	for(i=1 ;i< setTitle.length ; i++) print(",",setTitle[i]); printf(' ');

	printf('declare u: doit,countSetting,factorycurrent');
	printf('declare u: udummy, uuser, ufactory , ucurrent');
	printf('declare i: idummy, iuser, ifactory , icurrent');
	printf('declare s: sdummy, suser, sfactory , scurrent');
	printf('declare b: bdummy, buser, bfactory , bcurrent');
	
	
	printf('<br>','#**** OPEN FILE ****');
  	printf('outFile="/System/ZSCDef.txt"');
	writeFileOpen();

	SetCameraDefaultDescription( writef );


		printf('count=1');
		printf('LOOP:');
		printf('doit=0');
		printf('theType="UInt"');
		printf('umin=0');
		printf('umax=0');
		printf('countSetting=0');
		for( i=1 ; i< a.length ; i++)
		{ 			

 		// only can change a RW parameter 
  		if( a[i].host != "Write" ){
 				printf(" ");
 				printf('if count == ',i );
 				printf('WriteLine(fd," ")' );
				printf('WriteLine(fd,"**** ',a[i].desc,' ****" )' );
 				printf('WriteLine(fd,count,','")",',a[i].param,'," is READ only *************" )' );
 				printf('end');
 		 		continue;
 		}	
 			
 	 			printf(" ");
 				printf('if count == ', i );
  	 			printf("doit=1");
  	 			printf('param=',a[i].param);
  	 			printf(setTitle[0],'="',a[i].desc,'"');
  	 			
  	 			// write theType									
 				if( a[i].type == "String" || a[i].type=="DosName")
 					printf('theType="String"','<br>',"sdefault = ",a[i].def);
 				else if( (a[i].type=="Enum List") || (a[i].type=="UInt") )
 					printf("udefault = ",a[i].def);
 				else if( a[i].type=="SInt" )
 					printf('theType="SInt"','<br>',"default = ",a[i].def); 					
 				else 
 					printf('theType="Hex"','<br>',"udefault = ",a[i].def); 					
 					
 				
 				
 				// minimum, maximum range for either unsigned int, or signed int
 				// if this is a string, then it is a (max) hexadecimal value
				// if max is non-zero then there  min-max values are present
				// otherwise a zero indicates that there are no min-max values
				// if the 2nd clause is executed on a string, Netscape 3 complains
 				if (  (typeof(a[i].max)=="string") || (a[i].max != 0 )  )
 				if( (a[i].type=="Enum List") || (a[i].type=="Enum List Hex") || (a[i].type=="UInt") || (a[i].type=="Fixed") || (a[i].type=="BitMask") ){
 							printf("umin=",a[i].min );
 							printf("umax=",a[i].max );
 				}
 				else if( a[i].type=="SInt" ){
 							printf("min=",a[i].min );
 							printf("max=",a[i].max );
 				}
   				setting=1;								
 				for( theType in  a[i]   ){	
 				 		// paramNotThere(param, theObject  ) defined in GetCapabilities.js
 						// tag is a global object (defined via a function)
 						// that contains tags common to all parameters
						// this code picks up the extra tags which are NOT defined in tag
						// typically these are min,max, default or other  settings
						if( paramNotThere( theType , parameterTag) ){	
								printf( setTitle[setting]," = ",'"',theType ,'"' );

								// select the right variable type
 								if( a[i].type == "String" || a[i].type=="DosName")
 									printf( ssetVar[setting]," = ",a[i][theType] );
					 			else if( a[i].type=="SInt" )
				 					printf( isetVar[setting]," = ",a[i][theType] ); 				
 					 			else 
					 				printf( usetVar[setting]," = ",a[i][theType] );					

							setting = setting + 1;
							}
				}

				printf('countSetting=',setting-1);
 				printf('end '); 			

				
		}// end of this HUGE for loop
		printf(' ');
		
// start to examine SetCameraDefault()
	printf('Wait(100)');
	printf('if doit == 0');
	printf('count=count+1 ');	
	printf('goto LOOP');
	printf('end');
	printf(' ');
	
	printf('WriteLine(fd," ")' );
	printf('WriteLine(fd,"**** ",',setTitle[0],'," ****" )' );

 	printf(' if theType=="SInt"');
 	printf(' goto case_SINT');
 	printf(' end','<br>');

 	printf(' if theType=="String"');
 	printf(' goto case_STRING');
 	printf(' end','<br>');
	
	printf(' ' );
	printf('CASE_UINT:');
	// set user default to current state
	
	printf('# get from factory then current state ');

	printf('# user default = current state ');
	printf('status = GetCameraState(param,ucurrent)');
	printf('status1=SetCameraDefault(1,param,udummy)' );
	printf('status2=GetCameraDefault(1,param,uuser)' );

	printf('if theType != "Hex" ');
	printf('DisplayLine(count,")",param,"[",status,"] " , uuser )' );
	printf('Write(fd,count,")",param,"[",status,"][",status1,"][",status2,"] ")' );
	printf('WriteLine(fd,ucurrent," (current)  ",uuser," (user default)" )' );	
	printf('end','<br>');
	
	printf('if theType == "Hex" ');
	printf('bcurrent=ucurrent');
	printf('buser=uuser');
	printf('DisplayLine(count,")",param,"[",status,"] " , buser )' );
	printf('Write(fd,count,")",param,"[",status,"][",status1,"][",status2,"] ")' );
	printf('WriteLine(fd,bcurrent," (current)  ",buser," (user default)" )' );	
	printf('end','<br>');
	
	printf('<br>','if ucurrent != uuser');
	printf(' DisplayLine(" *** ??? ERROR ??? *** ") ');		
	printf(' WriteLine(fd," *** ??? ERROR ??? *** ") ');
	printf(' WriteLine(fd," *** check if a RAM value *** ") ');
	printf(' end ','<br>');

	// FACTORY default  
	printf('# user default = factory default ');
	printf('status=SetCameraDefault(0,param,udummy)' );
	printf('status1=GetCameraDefault(0,param,ufactory)' );
	printf('status2=GetCameraDefault(1,param,uuser)' );
	

	printf('if theType != "Hex" ');
	printf('DisplayLine(count,")",param,"[",status,"] " , uuser )' );
	printf('Write(fd,count,")",param,"[",status,"][",status1,"][",status2,"] ")' );
	printf('WriteLine(fd,ufactory,"(factory) ",uuser," (user default)" )' );	
	printf('end','<br>');
	
	printf('if theType == "Hex" ');
	printf('bfactory=ufactory');
	printf('buser=uuser');
	printf('DisplayLine(count,")",param,"[",status,"] " , buser )' );
	printf('Write(fd,count,")",param,"[",status,"][",status1,"][",status2,"] ")' );
	printf('WriteLine(fd,bfactory,"(factory) ",buser," (user default)" )' );	
	printf('end','<br>');
	
	printf('<br>','if ufactory != uuser');
	printf(' DisplayLine(" *** ??? ERROR ??? *** ") ');		
	printf(' WriteLine(fd," *** ??? ERROR ??? *** ") ');
	printf(' WriteLine(fd," *** check if a RAM value ***") ');

	printf(' end ','<br>');
	
	
	// set to maximum
	printf('<br>','if umax == 0 ' );
	printf('goto UINT_MINMAX_END');
	printf('end ');
	
	printf('# SET TO MINIMUM ');
	printf('status=SetCameraDefault(2,param,umin)' );		// take from umin variable
	printf('status1=GetCameraDefault(1,param,uvar)' );		// user default
	
	printf('if theType != "Hex" ');
	printf('DisplayLine(count,")",param,"[",status,"]" ,"[",status1,"] " , umin," (min)" )' );
	printf('WriteLine(fd,count,")",param,"[",status,"]" ,"[",status1,"] " , umin," (min) (",uvar,")" )' );
	printf('end');
	printf('if theType == "Hex" ');
	printf('bmin=umin ');	
	printf('bvar=uvar ');
	printf('DisplayLine(count,")",param,"[",status,"]" ,"[",status1,"] " , bmin," (min)" )' );
	printf('WriteLine(fd,count,")",param,"[",status,"]" ,"[",status1,"] " , bmin," (min) (",bvar,")" )' );
	printf('end');
	
	printf('<br>','if umin != uvar');
	printf(' DisplayLine(" *** ??? ERROR ??? *** ") ');	
	printf(' WriteLine(fd," *** ??? ERROR ??? *** ") ');
	printf(' WriteLine(fd," *** check if a RAM value *** ") ');
	printf(' end ','<br>');
	
	// set to maximum
	printf(' ');
	printf('# SET TO MAXIMUM ');
	printf('status=SetCameraDefault(2,param,umax)' ); 	// take from umax variable
	printf('status1=GetCameraDefault(1,param,uvar)' );	// user default
	printf(' if theType!="Hex"');	
	printf('DisplayLine(count,")",param,"[",status,"]" ,"[",status1,"] " , umax," (max)" )' );
	printf('WriteLine(fd,count,")",param,"[",status,"]" ,"[",status1,"] " , umax," (max) (",uvar,")" )' );
	printf('end');
	printf(' if theType=="Hex"');
	printf('bmax=umax');
	printf('bvar=uvar');	
	printf('DisplayLine(count,")",param,"[",status,"]" ,"[",status1,"] " , bmax," (max)" )' );
	printf('WriteLine(fd,count,")",param,"[",status,"]" ,"[",status1,"] " , bmax," (max) (",bvar,")" )' );
	printf('end');		
	
	printf('<br>','if umax != uvar');
	printf(' DisplayLine(" *** ??? ERROR ??? *** ") ');	
	printf(' WriteLine(fd," *** ??? ERROR ??? *** ") ');
	printf(' WriteLine(fd," *** check if a RAM value *** ") ');
	printf(' end ','<br>');
	
 	printf(' UINT_MINMAX_END: ');
	printf('# end of min,max stuff ');
// end of min, max stuff

//# **********CUT HERE ****************************************************
	for( n=1 ; n<=usetVar.length-1 ; n++){
			
	printf(' if countSetting>=',n);
	if(n==1) printf(' WriteLine(fd,"--------") ');	
	
	printf(' status=SetCameraDefault(2,param,',usetVar[n],')');
	printf(' status1=GetCameraDefault(1,param,uvar)  ');

	printf(' if theType!="Hex"');		
	printf(' DisplayLine(count,")",param,"[",status,"][",status2,"] ",', usetVar[n] ,'," (",',setTitle[n],',")" ) ');
	printf(' WriteLine(fd,count,")",param,"[",status,"][",status2,"] ",', usetVar[n] ,'," (",',setTitle[n],',"), (", uvar,")") ');
	printf('end');
	printf(' if theType=="Hex"');	
	printf(' bvar=uvar');
	printf(' bvar1=',usetVar[n] );
	printf(' DisplayLine(count,")",param,"[",status,"][",status2,"] ",bvar1," (",',setTitle[n],',")" ) ');
	printf(' WriteLine(fd,count,")",param,"[",status,"][",status2,"] ",bvar1," (",',setTitle[n],',"), (", bvar,")") ');
	printf('end');

	printf(' if ',usetVar[n],'!= uvar');
	printf(' WriteLine(fd," *** ??? ERROR ??? *** ")  ');
	printf(' WriteLine(fd," *** check if a RAM value *** ") ');
	printf(' end  ');
	printf(' end <br>');
	
	}

//# **********CUT HERE ****************************************************

	printf('# SET TO DEFAULT ');
	printf('status=RestoreCameraDefault(0,param)' );
	printf(' ');

	printf('goto CONTINUE');
	printf('# UINT finished  **************************************************');		


	
	printf('case_SINT:');

	// set user default to current state	
	printf('# user default = current state ');
	printf('status = GetCameraState(param,icurrent)');
	printf('status1=SetCameraDefault(1,param,idummy)' );
	printf('status2=GetCameraDefault(1,param,iuser)' );

	printf('DisplayLine(count,")",param,"[",status,"] " , ivar2 )' );
	printf('Write(fd,count,")",param,"[",status,"][",status1,"][",status2,"] ")' );
	printf('WriteLine(fd,uvar," (current) ",ivar2," (default) " )' );	
		
	printf('<br>','if icurrent != iuser');
	printf(' DisplayLine(" *** ??? ERROR ??? *** ") ');		
	printf(' WriteLine(fd," *** ??? ERROR ??? *** ") ');
	printf(' WriteLine(fd," *** check if a RAM value *** ") ');
	printf(' end ','<br>');

	// set user default to factory default 
	printf('# user default = factory default ');
	printf('status1= SetCameraDefault(0,param,idummy)' );
	printf('status = GetCameraDefault(0,param,ifactory)');
	printf('status2= GetCameraDefault(1,param,iuser)' );

	printf('DisplayLine(count,")",param,"[",status,"] " , iuser )' );
	printf('Write(fd,count,")",param,"[",status,"][",status1,"][",status2,"] ")' );
	printf('WriteLine(fd,iuser," (user) ",ifactory," (factory)" )' );	
		
	printf('<br>','if iuser != ifactory');
	printf(' DisplayLine(" *** ??? ERROR ??? *** ") ');		
	printf(' WriteLine(fd," *** ??? ERROR ??? *** ") ');
	printf(' WriteLine(fd," *** check if a RAM value *** ") ');
	printf(' end ','<br>');


	
	// set to minimum
	printf('<br>','if max == 0 ' );
	printf('goto SINT_MINMAX_END');
	printf('end');
	
	printf('status=SetCameraDefault(2,param,min)' );		// take from umin variable
	printf('status1=GetCameraDefault(1,param,iuser)' );		// user default
	
	printf('DisplayLine(count,")",param,"[",status,"] " , min," (min)" )' );
	printf('WriteLine(fd,count,")",param,"[",status,"] " , min," (min) (",iuser,")" )' );

	
	printf('<br>','if min != iuser');
	printf(' DisplayLine(" *** ??? ERROR ??? *** ") ');	
	printf(' WriteLine(fd," ***??? ERROR ??? *** ") ');
	printf(' WriteLine(fd," *** check if a RAM value *** ") ');
	printf(' end ','<br>');
	
	// set to maximum
	printf(' ');
	printf('status=SetCameraDefault(2,param,max)' ); 	// take from umax variable
	printf('status1=GetCameraDefault(1,param,ivar)' );	// user default

	printf('DisplayLine(count,")",param,"[",status,"] " , max," (max)" )' );
	printf('WriteLine(fd,count,")",param,"[",status,"] " , max," (max) (",iuser,")" )' );
	printf('end');
	
	printf('<br>','if max != iuser');
	printf(' DisplayLine(" *** ??? ERROR ??? *** ") ');	
	printf(' WriteLine(fd," *** ??? ERROR ??? *** ") ');
	printf(' WriteLine(fd," *** check if a RAM value *** ") ');
	printf(' end ','<br>');
 	printf(' SINT_MINMAX_END: ');
	printf('# end of SInt min,max stuff ');
// end of min, max stuff

//# **********SETTINGS  ****************************************************
	for( n=1 ; n<=isetVar.length-1 ; n++){
			
	printf(' if countSetting>=',n);
	if( n== 1)
	printf(' WriteLine(fd,"--------") ');
	
	printf(' status=SetCameraDefault(2,param,',isetVar[n],')');
	printf(' status1=GetCameraDefault(1,param,ivar)  ');

	printf(' DisplayLine(count,")",param,"[",status,"] " ,', isetVar[n] ,'," (",',setTitle[n],',")" ) ');
	printf(' WriteLine(fd,count,")",param,"[",status,"] " ,', isetVar[n] ,'," (",',setTitle[n],',"), (", uvar,")") ');

	printf(' if ',isetVar[n],'!= ivar');
	printf(' WriteLine(fd," *** ??? ERROR ??? *** ")  ');
	printf(' WriteLine(fd," *** check if a RAM value *** ") ');
	printf(' end  ');
	printf(' end <br>');
	
	}
//# **********SETTINGS  ****************************************************
	printf('# SET TO DEFAULT ');
	printf('status=RestoreCameraDefault(0,param)' );
	printf(' ');
	
	printf('goto CONTINUE');

	printf('# SINT finished  ***************************************************');		

	printf('case_STRING:');	
	printf('# user default = current state ');
	printf('status = GetCameraState(param,svar)');
	printf('status1=SetCameraDefault(1,param,sdummy)' );
	printf('status2=GetCameraDefault(1,param,svar2)' );
		
	printf('DisplayLine(count,")",param,"[",status,"] " , svar2 )' );
	printf('Write(fd,count,")",param,"[",status,"][",status1,"][",status2,"] ")' );
	printf('WriteLine(fd,svar," (current) ",svar2," (default)" )' );	

//# **********CUT HERE ****************************************************
	for( n=1 ; n<=ssetVar.length-1 ; n++){
			
	printf(' if countSetting>=',n);
	printf(' status=SetCameraDefault(2,param,',ssetVar[n],')');
	printf(' status1=GetCameraDefault(1,param,svar)  ');

	printf(' DisplayLine(count,")",param,"[",status,"][",status1 ,"]",', ssetVar[n] ,'," (",',setTitle[n],',")" ) ');
	printf(' WriteLine(fd,count,")",param,"[",status,"][",status1 ,"]",', ssetVar[n] ,'," (",',setTitle[n],',") (", svar,")") ');

	printf(' if ',ssetVar[n],'!= svar');
	printf(' WriteLine(fd," *** ??? ERROR ??? *** ")  ');
	printf(' WriteLine(fd," *** check if a RAM value *** ") ');
	printf(' end  ');
	printf(' end <br>');
	
	}

//# **********CUT HERE ****************************************************
		
	printf('# STRING finished  ************************************************');		


	printf('CONTINUE:');
	printf(' ');
	printf('if count<', a.length-1 );
	printf('count=count+1 ');
	printf('goto LOOP');
	printf('end');
	
	printf('# RESTORE USER DEFAULTS to FACTORY DEFAULTS ');
	printf('RestoreCameraDefault(0,"")');
	printf('# ');
	
	
	writeFileClose();
	printf('exitscript');

}

